﻿@model S4Store.Models.AfterSaleReport.AfterSaleDecoratingReportModel
@{
    Layout = null;

    //公式部分
    List<dynamic> list = new List<dynamic>();
    for (var dt = Model.BarModel.Start ?? DateTime.Now.AddMonths(-6); dt <= Model.BarModel.End; dt = dt.AddMonths(1))
    {
        var brandQuery = from s in Model.DayData
                         group s by s.CarBrandId into g
                         select new { key = g.Key, q = g, brandName = g.FirstOrDefault().CarBrand.Name };
        foreach (var item in brandQuery)
        {
            var brandOnMonthData = Model.DayData.Where(s => s.AddTime.Year == dt.Year && s.AddTime.Month == dt.Month && s.CarBrandId == item.key);
            if(brandOnMonthData.Count()==0)
            {
                list.Add(new
                {
                    时间 = dt,
                    品牌 = item.brandName,
                    销售额 = 0,
                    车辆数 = 0,
                });
            }else{
                list.Add(new
                {
                    时间 = dt,
                    品牌 = item.brandName,
                    销售额 = brandOnMonthData.Sum(o => o.YHCPXSZE_MaintenceSaleTotalVolume),
                    车辆数 = brandOnMonthData.Sum(o=>o.YHCPXSTC_MaintenceSaleNum)
                });
            }
        }
    }
    
    //图标部分
    S4Store.Models.Chart.Line ChartModel = new S4Store.Models.Chart.Line();
    ChartModel.Name = "各车型养护产品销售总额对比";
    ChartModel.IsRate = false;
    ChartModel.YAxis = "销售额";

    foreach (var item in list.GroupBy(o => o.时间))
    {
        ChartModel.XAxis.Add(item.Key.Year + "-" + item.Key.Month);
    }

    foreach (var item in list.GroupBy(o => o.品牌))
    {
        S4Store.Models.Chart.ChartSeries series = new S4Store.Models.Chart.ChartSeries();
        series.Name = item.Key;
        foreach (var item1 in item)
        {
            series.Data.Add(item1.销售额);
        }
        ChartModel.Serieses.Add(series);
    }
    S4Store.Models.Chart.ChartSeries seriestotal = new S4Store.Models.Chart.ChartSeries();
    seriestotal.Name = "全车系养护产品销售总额";
    foreach (var item in list.GroupBy(o => o.时间))
    {
        seriestotal.Data.Add(item.Sum(o => (decimal)o.销售额));
    }
    ChartModel.Serieses.Add(seriestotal);



    S4Store.Models.Chart.Line ChartModel1 = new S4Store.Models.Chart.Line();
    ChartModel1.Name = "各车型平均单车养护产品销售额对比";
    ChartModel1.IsRate = false;
    ChartModel1.YAxis = "销售额";

    foreach (var item in list.GroupBy(o => o.时间))
    {
        ChartModel1.XAxis.Add(item.Key.Year + "-" + item.Key.Month);
    }

    foreach (var item in list.GroupBy(o => o.品牌))
    {
        S4Store.Models.Chart.ChartSeries series = new S4Store.Models.Chart.ChartSeries();
        series.Name = item.Key;
        foreach (var item1 in item)
        {
            series.Data.Add(item1.车辆数 == 0 ? 0 : item1.销售额 / item1.车辆数);
        }
        ChartModel1.Serieses.Add(series);
    }
    S4Store.Models.Chart.ChartSeries seriestotal1 = new S4Store.Models.Chart.ChartSeries();
    seriestotal1.Name = "全车系平均单车养护产品销售额";
    foreach (var item in list.GroupBy(o => o.时间))
    {
        seriestotal1.Data.Add(item.Sum(o => (decimal)o.车辆数 == 0 ? 0 : (decimal)o.销售额 / (decimal)o.车辆数));
    }
    ChartModel1.Serieses.Add(seriestotal1);
}
<div style="height:100%; overflow:auto">
@Html.Action("ReportBar", "Common", Model.BarModel)
@*<div class="pagetitle">@(carbrandname)入厂台次过程KPI历史数据查询</div>*@
<div class="pageContent">
	
	<div class="tabs" currentIndex="0" eventType="click">
		<div class="tabsHeader">
			<div class="tabsHeaderContent">
				<ul>
					<li><a href="javascript:;"><span>各车型养护产品销售总额对比</span></a></li>
					<li><a href="javascript:;"><span>各车型平均单车养护产品销售额对比</span></a></li>
				</ul>
			</div>
		</div>
		<div class="tabsContent" >
			<div>
            @Html.Partial("../Chart/Line", ChartModel)
			</div>
			<div>
            @Html.Partial("../Chart/Line", ChartModel1)
			</div>
		</div>
		<div class="tabsFooter">
			<div class="tabsFooterContent"></div>
		</div>
	</div>
</div>

</div>